<?php

class ResponseMsg
{
    /**
     * 用于回复文本消息
     */
    public static function  responseText($postdata,$text){
        $texttpl='
            <xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
             </xml>  ';
        $text=sprintf($texttpl,$postdata->FromUserName,$postdata->ToUserName,time(),'text',$text);
        header('Content-Type:application/xml');
        echo $text;
    }

    public static function  responseImage($postdata,$meidaid){
        $texttpl='
            <xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Image>
                    <MediaId><![CDATA[%s]]></MediaId>
                </Image>
             </xml>  ';
        $text=sprintf($texttpl,$postdata->FromUserName,$postdata->ToUserName,time(),'image',$meidaid);
        header('Content-Type:application/xml');
        echo $text;
    }
    public static  function  responseLocation($postdata,$text){
        $texttpl='
            <xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
             </xml>  ';
        $text=sprintf($texttpl,$postdata->FromUserName,$postdata->ToUserName,time(),'text',$text);
        header('Content-Type:application/xml');
        echo $text;
    }

    public  static function outputMusic($postdata){
        $textTpl = '<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Music>%s</Music>
            <FuncFlag>0</FuncFlag>
        </xml>';

        $musicTpl = '
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <MusicUrl><![CDATA[%s]]></MusicUrl>
            <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
            ';
        $music1 = array(
            'title' => '一生所爱',
            'discription' => '为什么选这首歌呢？因为我的梦想是与一生所爱的人快乐一生。你的呢，亲爱的朋友？',
            'musicurl' => 'http://rubyeye-rubyeye.stor.sinaapp.com/song/%E5%8D%A2%E5%86%A0%E5%BB%B7-%E4%B8%80%E7%94%9F%E6%89%80%E7%88%B1.mp3',
            'hdmusicurl' => 'http://rubyeye-rubyeye.stor.sinaapp.com/song/%E5%8D%A2%E5%86%A0%E5%BB%B7-%E4%B8%80%E7%94%9F%E6%89%80%E7%88%B1.mp3'
        );
        $music = '';
        if (is_array($music1)){
            $music .= sprintf($musicTpl, $music1['title'], $music1['discription'], $music1['musicurl'], $music1['hdmusicurl']);
        }else{
            throw new Exception('$posts data structure wrong');
        }
        $text = sprintf($textTpl, $postdata->FromUserName, $postdata->ToUserName, time(), self::REPLY_TYPE_MUSIC, $music);
        header('Content-Type: application/xml');
        echo $text;
        // echo $music. "  ".$music1['title'];
    }

    /**
     * @param $postdata
     * @param $conarray
     * 回复图文消息
     */
    public  static function responseNews($postdata){
        $texttpl='
            <xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <ArticleCount><![CDATA[%s]]></ArticleCount>
                <Articles>%s</Articles>
             </xml>
            ';
        $itemtpl='
            <item>
                <Title><![CDATA[%s]]></Title>
                <Discription><![CDATA[%s]]></Discription>
                <PicUrl><![CDATA[%s]]></PicUrl>
                <Url><![CDATA[%s]]></Url>
            </item>
            ';
        $itemarray=array(
           array(
            'title'=>'这个世界你最美',
            'discription'=>'最大化影响力，是你人一生的意义',
            'picurl'=>'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2719065117,2668568953&fm=27&gp=0.jpg',
            'url'=>'http://www.xmedu.top/a/renshenglixiang/2018/0226/3.html'),
            array(
                'title'=>'这个世界你最丑',
                'discription'=>'最大化影响力，是你人一生的意义',
                'picurl'=>'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2719065117,2668568953&fm=27&gp=0.jpg',
                'url'=>'http://www.xmedu.top/a/renshenglixiang/2018/0226/3.html')
        );
        $items="";
        foreach($itemarray as $itemarr){
            $items.=sprintf($itemtpl,$itemarr['title'],$itemarr['discription'],$itemarr['picurl'],$itemarr['url']);
        }

        $text=sprintf($texttpl,$postdata->FromUserName,$postdata->ToUserName,time(),'news',2,$items);
        header('Content-Type:application/xml');
        echo $text;
    }
}